home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM3.lzh / LowLevelGraphics / Example2.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  8KB  |  234 lines

  1. /* Example 2                                                            */
  2. /* This example shows how to create a large Raster and a smaller        */
  3. /* display. We fill the Raster with a lot of pixels in seven different  */
  4. /* colours and by altering the RxOffset and RyOffset values in the      */
  5. /* RasInfo structure, the Raster is scrolled in all directions. This    */
  6. /* method to scroll a large drawing in full speed is used in many games */
  7. /* and was even used in my own racing game "Car".                       */
  8.  
  9.  
  10. #include <intuition/intuition.h>
  11. #include <graphics/gfxbase.h>
  12.  
  13.  
  14. #define RWIDTH   450 /* Raster 450 pixels wide.  */
  15. #define RHEIGHT  250 /* Raster 250 lines high.   */ 
  16.  
  17. /* The ViewPort is quite small, and is placed in the middle of the View: */
  18. #define DWIDTH   200 /* Display 200 pixels wide. */ 
  19. #define DHEIGHT  100 /* Display 100 lines high.  */
  20. #define DXOFFSET  60 /* DxOffset 60 pixels.      */
  21. #define DYOFFSET  50 /* DyOffset 50 lines.       */
  22.  
  23. #define DEPTH      3 /* 3 BitPlanes should be used, gives eight colours. */
  24. #define COLOURS    8 /* 2^3 = 8                                          */
  25.  
  26. #define SPEED      1 /* How many pixels the Raster should be scrolled */
  27.                      /* every time.                                   */
  28.  
  29.  
  30. struct IntuitionBase *IntuitionBase;
  31. struct GfxBase *GfxBase;
  32.  
  33.  
  34. struct View my_view;
  35. struct View *my_old_view;
  36. struct ViewPort my_view_port;
  37. struct RasInfo my_ras_info;
  38. struct BitMap my_bit_map;
  39. struct RastPort my_rast_port;
  40.  
  41.  
  42. UWORD my_color_table[] =
  43. {
  44.   0x000, /* Colour 0, Black       */
  45.   0x800, /* Colour 1, Red         */
  46.   0xF00, /* Colour 2, Light red   */
  47.   0x080, /* Colour 3, Green       */
  48.   0x0F0, /* Colour 4, Light green */
  49.   0x008, /* Colour 5, Blue        */
  50.   0x00F, /* Colour 6, Light Blue  */
  51.   0xFFF, /* Colour 7, White       */
  52. };
  53.  
  54.  
  55. void clean_up();
  56. void main();
  57.  
  58.  
  59. void main()
  60. {
  61.   SHORT deltaX = SPEED;
  62.   SHORT deltaY = SPEED;
  63.   UWORD *pointer;
  64.   int loop;
  65.   
  66.   
  67.  
  68.   /* Open the Intuition library: */
  69.   IntuitionBase = (struct IntuitionBase *)
  70.     OpenLibrary( "intuition.library", 0 );
  71.   if( !IntuitionBase )
  72.     clean_up( "Could NOT open the Intuition library!" );
  73.  
  74.   /* Open the Graphics library: */
  75.   GfxBase = (struct GfxBase *)
  76.     OpenLibrary( "graphics.library", 0 );
  77.   if( !GfxBase )
  78.     clean_up( "Could NOT open the Graphics library!" );
  79.  
  80.  
  81.   /* Save the current View, so we can restore it later: */
  82.   my_old_view = GfxBase->ActiView;
  83.  
  84.  
  85.   /* 1. Prepare the View structure, and give it a pointer to */
  86.   /*    the first ViewPort:                                  */
  87.   InitView( &my_view );
  88.   my_view.ViewPort = &my_view_port;
  89.  
  90.  
  91.   /* 2. Prepare the ViewPort structure, and set some important values: */
  92.   InitVPort( &my_view_port );
  93.   my_view_port.DWidth = DWIDTH;        /* Set the width.                */
  94.   my_view_port.DHeight = DHEIGHT;      /* Set the height.               */
  95.   my_view_port.DxOffset = DXOFFSET;    /* Set the display X offset.     */
  96.   my_view_port.DyOffset = DYOFFSET;    /* Set the display Y offset.     */
  97.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  98.   my_view_port.Modes = NULL;           /* Low resolution.               */
  99.  
  100.  
  101.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  102.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  103.   if( my_view_port.ColorMap == NULL )
  104.     clean_up( "Could NOT get a ColorMap!" );
  105.  
  106.   /* Get a pointer to the colour map: */
  107.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  108.  
  109.   /* Set the colours: */
  110.   for( loop = 0; loop < COLOURS; loop++ )
  111.     *pointer++ = my_color_table[ loop ];
  112.  
  113.  
  114.   /* 4. Prepare the BitMap: */
  115.   InitBitMap( &my_bit_map, DEPTH, RWIDTH, RHEIGHT );
  116.  
  117.   /* Allocate memory for the Raster: */ 
  118.   for( loop = 0; loop < DEPTH; loop++ )
  119.   {
  120.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( RWIDTH, RHEIGHT );
  121.     if( my_bit_map.Planes[ loop ] == NULL )
  122.       clean_up( "Could NOT allocate enough memory for the raster!" );
  123.  
  124.     /* Clear the display memory with help of the Blitter: */
  125.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( RWIDTH, RHEIGHT ), 0 );
  126.   }
  127.  
  128.   
  129.   /* 5. Prepare the RasInfo structure: */
  130.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  131.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  132.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  133.                                     /* of the display.                   */
  134.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  135.                                     /* RasInfo structure is necessary.   */
  136.  
  137.   /* 6. Create the display: */
  138.   MakeVPort( &my_view, &my_view_port );
  139.   MrgCop( &my_view );
  140.  
  141.  
  142.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  143.   InitRastPort( &my_rast_port );
  144.   my_rast_port.BitMap = &my_bit_map;
  145.   
  146.  
  147.   /* 8. Show the new View: */
  148.   LoadView( &my_view );
  149.  
  150.  
  151.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  152.   SetDrMd( &my_rast_port, JAM1 );
  153.   /* Draw 10000 pixels in seven different colours, randomly. */ 
  154.   for( loop = 0; loop < 10000; loop++ )
  155.   {
  156.     /* Set FgPen's colour (1-7, 0 used for the the background). */
  157.     SetAPen( &my_rast_port, rand() % (COLOURS-1) + 1 );
  158.     /* Write a pixel somewere on the display: */
  159.     WritePixel( &my_rast_port, rand() % RWIDTH, rand() % RHEIGHT );
  160.   }
  161.  
  162.  
  163.   /* Scroll the Raster in all directions for a little while: */
  164.   for( loop = 0; loop < 5000; loop++ )
  165.   {
  166.     my_ras_info.RxOffset += deltaX;
  167.     my_ras_info.RyOffset += deltaY;
  168.  
  169.     /* The Raster is moved in one direction until the other side is */
  170.     /* reached were we change the direction:                        */ 
  171.  
  172.     /* Have we reached the left side? */
  173.     if( my_ras_info.RxOffset <= 0 )
  174.       deltaX = SPEED;
  175.     /* Have we reached the right (Raster width - Display width) side? */
  176.     if( my_ras_info.RxOffset >= RWIDTH - DWIDTH )
  177.       deltaX = -SPEED;
  178.  
  179.     /* Have we reached the top side? */
  180.     if( my_ras_info.RyOffset <= 0 )
  181.       deltaY = SPEED;
  182.     /* Have we reached the bottom (Raster height - Display height) side? */
  183.     if( my_ras_info.RyOffset >= RHEIGHT - DHEIGHT )
  184.       deltaY = -SPEED;
  185.  
  186.  
  187.     /* Recalculate the display instructions: (If you change any values */
  188.     /* in the display structures the Amiga have to recalculate the     */
  189.     /* entire display instructions. You must therefore call all three  */
  190.     /* display functions: MakeVPort(), MrgCop() and LoadView().)       */
  191.     MakeVPort( &my_view, &my_view_port );
  192.     MrgCop( &my_view );
  193.     LoadView( &my_view );
  194.   }
  195.  
  196.  
  197.   /* 9. Restore the old View: */
  198.   LoadView( my_old_view );
  199.  
  200.  
  201.   /* Free all allocated resources and leave. */
  202.   clean_up( "THE END" );
  203. }
  204.  
  205.  
  206. /* Returns all allocated resources: */
  207. void clean_up( message )
  208. STRPTR message;
  209. {
  210.   int loop;
  211.  
  212.   /* Free automatically allocated display structures: */
  213.   FreeVPortCopLists( &my_view_port );
  214.   FreeCprList( my_view.LOFCprList );
  215.   
  216.   /* Deallocate the display memory, BitPlane for BitPlane: */
  217.   for( loop = 0; loop < DEPTH; loop++ )
  218.     if( my_bit_map.Planes[ loop ] )
  219.       FreeRaster( my_bit_map.Planes[ loop ], RWIDTH, RHEIGHT );
  220.  
  221.   /* Deallocate the ColorMap: */
  222.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  223.  
  224.   /* Close the Graphics library: */
  225.   if( GfxBase ) CloseLibrary( GfxBase );
  226.  
  227.   /* Close the Intuition library: */
  228.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  229.  
  230.   /* Print the message and leave: */
  231.   printf( "%s\n", message ); 
  232.   exit();
  233. }
  234.